package org.jsola.hr.web;

import org.jsola.common.FileKit;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.TreatmentDisposeTemplateAddDTO;
import org.jsola.hr.dto.TreatmentDisposeTemplateUpdateDTO;
import org.jsola.hr.entity.TreatmentDisposeTemplateDO;
import org.jsola.hr.query.TreatmentDisposeTemplateQuery;
import org.jsola.hr.service.ITreatmentDisposeTemplateService;
import org.jsola.hr.vo.FormGroupVO;
import org.jsola.hr.vo.TreatmentDisposeTemplateListVO;
import org.jsola.hr.vo.TreatmentDisposeTemplateVO;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import javax.validation.Valid;


/**
 * 待遇处理模板
 *
 * @author zhr
 */
@RestController("hrTreatmentDisposeTemplateController")
@RequestMapping(HrConstants.API_PATH + "/v1/treatmentDisposeTemplates")
public class TreatmentDisposeTemplateController implements ITreatmentDisposeTemplateControllerAPI {

    @Autowired
    private ITreatmentDisposeTemplateService treatmentDisposeTemplateService;

    @Override
    @PostMapping
    public Result<TreatmentDisposeTemplateVO> save(@Valid @RequestBody TreatmentDisposeTemplateAddDTO treatmentDisposeTemplateAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增待遇处理模板权限校验

        return Result.success(
                treatmentDisposeTemplateService.save(treatmentDisposeTemplateAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody TreatmentDisposeTemplateUpdateDTO treatmentDisposeTemplateUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改待遇处理模板权限校验

        int count = treatmentDisposeTemplateService.update(treatmentDisposeTemplateUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }


    @Override
    @DeleteMapping(value = "/{treatmentDisposeTemplateId}")
    public Result<Integer> delete(@PathVariable String treatmentDisposeTemplateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除待遇处理模板权限校验

        int count = treatmentDisposeTemplateService.deleteByIds(tokenUser, treatmentDisposeTemplateId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] treatmentDisposeTemplateIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除待遇处理模板权限校验

        int count = treatmentDisposeTemplateService.deleteByIds(tokenUser, treatmentDisposeTemplateIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{treatmentDisposeTemplateId}")
    public Result<TreatmentDisposeTemplateVO> findById(@PathVariable String treatmentDisposeTemplateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看待遇处理模板权限校验

        return Result.success(
                treatmentDisposeTemplateService.selectById(treatmentDisposeTemplateId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<TreatmentDisposeTemplateListVO>> listAll(@Valid TreatmentDisposeTemplateQuery treatmentDisposeTemplateQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看待遇处理模板权限校验

        return Result.success(
                treatmentDisposeTemplateService.select(treatmentDisposeTemplateQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<TreatmentDisposeTemplateListVO>> page(@Valid TreatmentDisposeTemplateQuery treatmentDisposeTemplateQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                treatmentDisposeTemplateService.selectPage(treatmentDisposeTemplateQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/selectSiteDefaultTemplate")
    public Result<List<TreatmentDisposeTemplateVO>> selectSiteDefaultTemplate() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                treatmentDisposeTemplateService.selectSiteDefaultTemplate(tokenUser)
        );
    }

    @Override
    @GetMapping("/selectCustomTemplatePage")
    public Result<Page<TreatmentDisposeTemplateVO>> selectCustomTemplatePage(TreatmentDisposeTemplateQuery templateQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                treatmentDisposeTemplateService.selectCustomTemplatePage(templateQuery, tokenUser)
        );
    }

    @Override
    @GetMapping("/selectAllTemplateBySite")
    public Result<List<TreatmentDisposeTemplateDO>> selectAllTemplateBySite(
            @RequestParam(value = "name", required = false) String name) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                treatmentDisposeTemplateService.selectAllTemplateBySite(name, tokenUser)
        );
    }

    @Override
    @GetMapping("/selectTemplateInfoById/{templateId}")
    public Result<TreatmentDisposeTemplateVO> selectTemplateInfoById(@PathVariable String templateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                treatmentDisposeTemplateService.selectTemplateInfoById(templateId, tokenUser)
        );
    }

    @Override
    @GetMapping("/generateExcel/{templateId}")
    public ResponseEntity<Resource> generateExcel(@PathVariable String templateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验
        String excelFilePath = treatmentDisposeTemplateService.generateExcel(templateId, tokenUser);
        String filenames = FileKit.getNameAndEncode(excelFilePath);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filenames + "\"")
                .header(HttpHeaders.CONTENT_TYPE, "application/vnd.ms-excel")
                .body(new FileSystemResource(excelFilePath));
    }

    @Override
    @GetMapping("/selectProcessChildByTemplateId/{templateId}")
    public Result<List<FormGroupVO>> selectProcessChildByTemplateId(@PathVariable String templateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
                treatmentDisposeTemplateService.selectProcessChildByTemplateId(templateId, tokenUser)
        );
    }

    @Override
    @PostMapping("/copyTemplate/{templateId}")
    public Result<String> copyTemplate(@PathVariable String templateId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        treatmentDisposeTemplateService.copyTemplate(templateId, tokenUser);
        return Result.success();
    }

}

